<?php

declare(strict_types=1);

namespace App\Command;

use App\Model\JxXApiUser;
use App\Model\XApiCompanyExtend;
use App\Model\XApiUser;
use App\Model\ZaXApiCompanyExtend;
use App\Service\UserService;
use FooRedis;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Hyperf\Database\Model\ModelNotFoundException;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Utils\Coroutine;
use Hyperf\Utils\Exception\ParallelExecutionException;
use Hyperf\Utils\Parallel;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputArgument;

/**
 * @Command
 */
class CompareExistUser extends HyperfCommand
{


    private $container;
    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;
    public function __construct(ContainerInterface $container,LoggerFactory $loggerFactory)
    {
        parent::__construct('compare:user');
        $this->container = $container;
        $this->logger = $loggerFactory->get('log', 'dbAction');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('compare');
    }

    /**
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function handle()
    {

        XApiUser::query()->orderBy("id","desc")->chunkById(2000,function($users){

            $parallel = new Parallel(100);

            foreach ($users as $user){

                $parallel->add(function () use ($user) {
                    try{
                        JxXApiUser::query()->findOrFail($user->id);
                    }catch (ModelNotFoundException $exception){

                        $this->logger->info("找不到的userID: " .$user->id);
                    }
                    return Coroutine::id();
                });

            }

            try{
                $results = $parallel->wait();
            } catch(ParallelExecutionException $e){
                // $e->getResults() 获取协程中的返回值。
                // $e->getThrowables() 获取协程中出现的异常。
            }
        });

    }

}
